//
//  HomeConnect.m
//  AirQuality
//
//  Created by Po on 15/6/19.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "HomeConnect.h"
#import "RJCityConnectView.h"
#import "RJCountryConnectView.h"

@interface HomeConnect () <RJHomeBottomFatherViewMoveDelegate>

@property (retain, nonatomic) RJCityConnectView * cityView;
@property (retain, nonatomic) RJCountryConnectView * countryView;

@property (assign, nonatomic) CGFloat trueScrollHeight;                  //scrollView contentView

@property (assign, nonatomic) NetRequestStatus cityNetRequestStatus;
@property (assign, nonatomic) NetRequestStatus countryNetRequestStatus;


@end

@implementation HomeConnect

- (instancetype)initWithFrame:(CGRect)frame vc:(UIViewController *)vc
{
    self = [super initWithFrame:frame];
    if (self) {
        self.vc = vc;
        _cityNetRequestStatus = NetRequestStatusDoing;
        _countryNetRequestStatus = NetRequestStatusDoing;
        [self addSubview:[self getCityView]];
        [self addSubview:[self getCountryView]];
        _trueScrollHeight = frame.size.height;
        _trueScrollHeight -= 30;
        [self showLoadingView];
    }
    return self;
}

#pragma mark - function
- (void)checkNetRequest
{
    if ((_cityNetRequestStatus != NetRequestStatusDoing)
        && (_countryNetRequestStatus != NetRequestStatusDoing)) {
        if ((_cityNetRequestStatus == NetRequestStatusFailure) || (_countryNetRequestStatus == NetRequestStatusFailure) ) {
            [RJTools showTitle:@"网络异常" msg:@"网络异常，请点击重试！" cancelString:@"取消" confirmString:@"重试" confirmBlock:^{
                _cityNetRequestStatus = NetRequestStatusDoing;
                _countryNetRequestStatus = NetRequestStatusDoing;
                [_cityView initNet];
                [_countryView initNet];
            } vc:self.vc];
        }
        [self freeLoadingView];
    }
}
#pragma mark - delegate
- (void)touchMove:(BOOL)isLeft
{
    if (self.moveDelegate) {
        [self.moveDelegate touchMove:isLeft];
    }
}

- (void)HomeBottomFatherView:(RJHomeBottomFatherView *)vc netFinish:(NetRequestStatus)status
{
    if (vc == _cityView) {
        _cityNetRequestStatus = status;
    } else {
        _countryNetRequestStatus = status;
    }
    
    [self checkNetRequest];
}

#pragma mark - getter
- (RJCityConnectView *)getCityView
{
    if (!_cityView) {
        CGRect rect = self.bounds;
        rect.size.height = rect.size.height/ 2;
        _cityView = [[RJCityConnectView alloc] initWithFrame:rect];
        [_cityView setMoveDelegate:self];
    }
    return _cityView;
}

- (RJCountryConnectView *)getCountryView
{
    if (!_countryView) {
        CGRect rect = self.bounds;
        rect.size.height = rect.size.height / 2;
        rect.origin.y = rect.size.height  - 20;
        _countryView = [[RJCountryConnectView alloc] initWithFrame:rect];
        [_countryView setMoveDelegate:self];
    }
    return _countryView;
}
@end
